package proj1;

/**
 * Parsing exception. For simplicity, this is used for both lexical errors as
 * well as parsing errors. A more complete design would include a hierarchy of
 * specific error types.
 *
 * Developed by Adam Conover (2012) and modified by Josh Dehlinger (2013), used with permission.
 *
 */
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;


public class CompilerException extends Exception{
	 /**
     * Instantiates a new CompilerException.
     *
     * @param errorMessage the error message to be printed
     */
    public CompilerException(String errorMessage) {
        super(errorMessage);
    }

    /**
     * Gets the error message.
     *
     * @return the error message
     */
    public String getErrorMessage() {
        return super.getMessage();
    }
    
 // Global variable for the current token
    public static String currentToken;
    // Instance of the lexical analyzer
    public static LexAnalyzer Lexer;
    
     
    // It is assumed that the first argument provided is the name of the source file
    // that is to be "compiled". 
    public static void main(String[] args) throws FileNotFoundException, IOException, CompilerException {
         // Initializations
     Lexer = new LexAnalyzer();
     SynAnalyzer Parser = new SynAnalyzer();
     
     // Array for all test files
     ArrayList<String> testCases = new ArrayList<String>();
     testCases.add("Test1.txt");
     testCases.add("Test2.mkd");
     
     
     
      // variable for the file name
      // Open the file
       String fileName = testCases.get(0);
       //FileInputStream fstream = new FileInputStream(fileName);
       //BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
       
       FileReader fr = new FileReader(args[0]);
       BufferedReader br = new BufferedReader(fr);
       
       // check test case extension for .mkd
       String extension = fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length());

       String markdown = "mkd";
       if (!extension.equals(markdown)) {
         System.out.println("Choose a markdown file!");
       }
       String strLine;
       String content;
       String docum = "";
       //Read File Line By Line & create string of entire file
       while ((strLine = br.readLine()) != null)
         docum += strLine;
       
     
      System.out.println(docum);
      // Read each line in the source file to be compiled as a unique sentence to 
      // check against the grammar. 
      while(Lexer.getCurrentPosition() < docum.length()){ 
        content = docum.substring(Lexer.getCurrentPosition(), docum.length());
        // Get the first token
        Lexer.getNextToken();
       
        // Parse the given sentence against the given grammar. We assume that the
        // html, <html>, production is the start state. 
        Parser.document();
     
        
      } // close while loop
      // Close the file and clean up
      br.close(); 
     
      } // close main
   

}
